﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Grafo
{
     partial class Form1 : Form
    {

        public static List<Raw> lista = new List<Raw>();
        

        Raw raw;


        public Form1()
        {
            this.ShowInTaskbar = true;
            InitializeComponent();
        }

        #region FORM
        private void Form1_Load(object sender, EventArgs e)
        {
            textBox1.Text = Environment.CurrentDirectory;
        }

        #region Tab1
        private void button1_Click(object sender, EventArgs e)
        {
            tabControl1.SelectTab(1);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            tabControl1.SelectTab(2);
        }
        #endregion



        private void button3_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderBrowse = new FolderBrowserDialog();
            folderBrowse.SelectedPath = textBox1.Text;
            folderBrowse.ShowNewFolderButton = false;
            folderBrowse.ShowDialog(this);
            textBox1.Text = folderBrowse.SelectedPath;
        }
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (System.IO.Directory.Exists(textBox1.Text))
            {
                textBox1.BackColor = Color.White;
                lista.Clear();
                foreach (string arquivo in System.IO.Directory.GetFiles(textBox1.Text))
                {

                    if (arquivo.Contains(".txt"))
                    {
                        lista.Add(LoadFiles(arquivo));
                    }
                    label1.Text = lista.Count + " Encontrados.";
                    button1.Enabled = true;
                    button2.Enabled = true;

                }
            }

            else
            {
                textBox1.BackColor = Color.Red;
                label1.Text = "...";

                button1.Enabled = false;
                button2.Enabled = false;


                return;
            }
            Start();
        }
        private Raw LoadFiles(string caminho)
        {
            System.IO.FileInfo fi = new System.IO.FileInfo(caminho);
            Raw raw = new Raw();


            raw.nome = fi.Name.Remove(fi.Name.Length - 4);
            raw.nome = raw.nome.Trim();

            System.IO.StreamReader tr = new System.IO.StreamReader(caminho);
            string elementos = "";
            do
            {
                elementos += tr.ReadLine() + "|";
            } while (!tr.EndOfStream);

            raw.elementos = elementos.Remove(elementos.Length - 1).Split('|');
           


            return raw;
        }

        #endregion

        
        public void Start()
        {
            matrizDraw();
            matrizFill();
            //listaAc();
        }

        #region MATRIZ
        private void matrizDraw()
        {
            dataGridView1.Columns.Clear();
            dataGridView1.Rows.Clear();
            raw = new Raw();

            string[] l = raw.getElementos(lista);
            dataGridView1.Columns.Add("item", "");
            dataGridView1.Rows.Add();



            foreach (var item in l)
            {
                dataGridView1.Columns.Add(item, "");
                dataGridView1.Rows.Add();
            }
            for (int i = 1; i <= l.Length; i++)
            {
                dataGridView1[0, i].Value = l[i - 1];
                dataGridView1[i, 0].Value = l[i - 1];
                dataGridView1[0, i].Style.BackColor = Color.DarkBlue;
                dataGridView1[i, 0].Style.BackColor = Color.DarkBlue;
                dataGridView1[0, i].Style.ForeColor = Color.White;
                dataGridView1[i, 0].Style.ForeColor = Color.White;
                //dataGridView1[0, i].Style.Font = new System.Drawing.Font(dataGridView1.Font, FontStyle.Bold);
                //dataGridView1[i, 0].Style.Font = new System.Drawing.Font(dataGridView1.Font, FontStyle.Bold);
           }
            for (int col = 1; col < dataGridView1.Columns.Count; col++)
            {
                for (int lin = 1; lin < dataGridView1.Rows.Count; lin++)
                {
                    dataGridView1[col, lin].Value = "";
                    dataGridView1[col, lin].Style.BackColor = Color.LightGray;
                    dataGridView1[col, lin].Style.ForeColor = Color.White;
                }
            }
            


            for (int i = 0; i < dataGridView1.Columns.Count; i++)
            {
                dataGridView1.Columns[i].Width = 20;
            }

        }
        private void matrizFill()
        {
            foreach (var n in lista)
            {
                for (int i = 1; i < dataGridView1.Columns.Count; i++)
                {
                    if (dataGridView1[i, 0].Value.ToString() == n.nome)
                    {
                        foreach (var col in n.elementos)
                        {
                            for (int j = 1; j < dataGridView1.Rows.Count; j++)
                            {
                                if (dataGridView1[0, j].Value.ToString() == dataGridView1[i, 0].Value.ToString())
                                {
                                    dataGridView1[i, j].Value = "X";
                                    dataGridView1[i, j].Style.BackColor = Color.Tomato;
                                    dataGridView1[i, j].Style.ForeColor = Color.White;                                    
                                }
                                else if (dataGridView1[0, j].Value.ToString() == col)
                                {
                                    dataGridView1[i, j].Value = col;
                                    dataGridView1[i, j].Style.BackColor = Color.WhiteSmoke;
                                    dataGridView1[i, j].Style.ForeColor = Color.Blue;

                                } 
                                /*else
                                {
                                    dataGridView1[i, j].Value = "0";
                                    dataGridView1[i, j].Style.BackColor = Color.LightCyan;
                                    dataGridView1[i, j].Style.ForeColor = Color.White;
                                }*/
                            }
                        }
                    }                   
                }
            }
            for (int i = 0; i < dataGridView1.Columns.Count; i++)
            {
                
            }
        }

        #endregion

        #region Lista

        public void listaAc(Raw lista)
        {
            visitar(lista.nome);
        }
        public string visitar (string n)
        {

            return "";
        }
        #endregion

        private void button4_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();
            f2.ShowDialog();
        }
    }
}